// export const HANDLERS= {
//     "STUDENT_TO_EDU_COUNSELOR_NOTICE": StudentToEduCounselorNoticeHandler,
//     "SEND_TO_ONE_MESSAGE": SendToOneMessageHandler,
//     "EDU_COUNSELOR_TO_STUDENT_NOTICE": EduCounselorToStudentNoticeHandler,
//     "RECEIVE_MESSAGE":ReceiveMessageHandler,
//     "RECEIVE_NOTICE":ReceiveNoticeHandler
// }

import {openChatBox,waitingLayer} from "./universityIntro.js";

export const HANDLERS = new Map();
HANDLERS.set("STUDENT_TO_EDU_COUNSELOR_NOTICE",StudentToEduCounselorNoticeHandler)
HANDLERS.set("SEND_TO_ONE_MESSAGE",SendToOneMessageHandler)
HANDLERS.set("EDU_COUNSELOR_TO_STUDENT_NOTICE",EduCounselorToStudentNoticeHandler)
HANDLERS.set("FORWARD_TO_ONE_MESSAGE",ReceiveMessageHandler)
HANDLERS.set("FORWARD_TO_ONE_NOTICE",ReceiveNoticeHandler)    //收到服务器转发的消息

function StudentToEduCounselorNoticeHandler(ws,messageJSON) {

    ws.send(JSON.stringify(messageJSON));
}

function SendToOneMessageHandler(ws,messageJSON) {
    ws.send(JSON.stringify(messageJSON));
}

function EduCounselorToStudentNoticeHandler() {

}

function ReceiveMessageHandler(ws,messageJSON) {
    let content = messageJSON.body.content;
    let formUser = messageJSON.body.fromUser;
    let selectStr = "div[class='layui-layer-title']:contains("+formUser+")"
    let $formUser = $(selectStr)
    let messageShowBox = $formUser.next().find('#messageShowBox')
    let oppositeMessageObj = $('<div/>', {'class': 'oppositeMessage'});
    oppositeMessageObj.html(content);
    let timeObj = $('<div/>', {'class': 'oppositeMessage'});
    timeObj.html(formUser+" "+new Date().toLocaleTimeString());
    messageShowBox.append(timeObj);
    messageShowBox.append(oppositeMessageObj);

}

function ReceiveNoticeHandler(ws,messageJSON) {
     // private String msgID;
     // private String from;
     // private String content;
    let from = messageJSON.body.from;
    let content = messageJSON.body.content;
    if (content==="REQUEST_CHAT"){
        layui.use('layer', function () {
            const layer = layui.layer;
            layer.confirm(from + "想请教您一些问题", {
                cancel: function () {
                    const refuseMessage = {
                        "type":"EDU_COUNSELOR_TO_STUDENT_NOTICE",
                        "body":{
                            "toUser":from,
                            "msgID":"123123",
                            "content":"REFUSE_CHAT"
                        }
                    };
                    ws.send(JSON.stringify(refuseMessage));
                },
                    btn: ['接受', '拒绝']
                },
                function (index) {
                    const acceptMessage = {
                        "type":"EDU_COUNSELOR_TO_STUDENT_NOTICE",
                        "body":{
                            "toUser":from,
                            "msgID":"123123",
                            "content":"ACCEPT_CHAT"
                        }
                    };
                    ws.send(JSON.stringify(acceptMessage));
                    openChatBox(from);
                    layer.close(index);
                }, function (index) {
                    const refuseMessage = {
                        "type":"EDU_COUNSELOR_TO_STUDENT_NOTICE",
                        "body":{
                            "toUser":from,
                            "msgID":"123123",
                            "content":"REFUSE_CHAT"
                        }
                    };
                    ws.send(JSON.stringify(refuseMessage));
                    layer.close(index)
                })
        })

    }else if(content==="REFUSE_CHAT"){
        layer.close(waitingLayer);
        layer.msg("该导生拒绝聊天");
        localStorage.setItem("cancel","false");

    }else if (content==="ACCEPT_CHAT"){
        layer.close(waitingLayer);
        localStorage.setItem("cancel","false");
        openChatBox(from);
    }else {
        console.error("[ReceiveNoticeHandler] Content not Found")
    }
}